package com.softgroup.breath.easyecgproject.myapi;

import java.io.BufferedReader;
import java.io.CharArrayWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2017/5/16.
 */

public class FormatCast {
    public static byte[] getHexBytes(String message) {
        int len = message.length() / 2;
        char[] chars = message.toCharArray();
        String[] hexStr = new String[len];
        byte[] bytes = new byte[len];
        for (int i = 0, j = 0; j < len; i += 2, j++) {
            hexStr[j] = "" + chars[i] + chars[i + 1];
            bytes[j] = (byte) Integer.parseInt(hexStr[j], 16);
        }
        return bytes;
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

    public static String getDate(boolean isCast) {
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        SimpleDateFormat format1=new SimpleDateFormat("HHmmss:SSS");
        Date currentDate = new Date(System.currentTimeMillis());
        String currentDataString = format.format(currentDate);
        String currentDataString1=format1.format(currentDate);
        if (!isCast){
            return currentDataString1;
        }
        String st = "";
        for (int i = 0; i < 12; i = i + 2) {
            String s = Integer.toHexString(Integer.parseInt(currentDataString.substring(i, i + 2)));
            if (s.length() == 1) {
                s = 0 + s;
            }
            st = st + s;
        }
        return st;
//        Toast.makeText(context, currentDataString, Toast.LENGTH_SHORT).show();
    }
    public static String getFullDate(){
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        Date currentDate = new Date(System.currentTimeMillis());
        return format.format(currentDate);
    }

    public static char[] readChars(BufferedReader in){

        int buffSize=1024;
        CharArrayWriter out=new CharArrayWriter(buffSize);
        char[] chars = new char[buffSize];
        int size = 0;
        char[] content=null;
        try {
            while ((size = in.read(chars)) != -1) {
                out.write(chars, 0, size);
            }
            content = out.toCharArray();

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            return content;
        }
    }
}
